package com.infore.dataAudit.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.activiti.engine.task.Task;

import com.infore.dataAudit.dto.Project;
import com.infore.dataAudit.entity.TDataAudit;
import com.infore.dataAudit.entity.TDataAuditDetail;
import com.infore.dataAudit.enums.AuditStatus;
import com.infore.dataAudit.vo.DataAuditBO;
import com.infore.dataAudit.vo.DataAuditHeadBO;

/**
 * 数据审核 转换工具类
 * @author cys
 *
 */
public class AuditDataCovertUtil {
    
    //审核数据各因子字段返回封装
    private static String[] headProjs = {"监测时间","水温(°C)","pH(无量纲)","溶解氧(mg/L)","电导率(uS/cm)","浊度(NTU)",
                "高锰酸盐指数(mg/L)","氨氮(mg/L)","总磷(mg/L)","总氮(mg/L)"};
    
    
    
    
    public static String[] getHeadProjs() {
        return headProjs;
    }

    public static void setHeadProjs(String[] headProjs) {
        AuditDataCovertUtil.headProjs = headProjs;
    }
    
    public static List<DataAuditHeadBO> getDataAuditHead(List<Project> projectInfos){
        List<DataAuditHeadBO> headList = new ArrayList<>();
        DataAuditHeadBO bo = null;
        bo = new DataAuditHeadBO();
        bo.setLabel1("监测时间");
        bo.setProp("time");
        headList.add(bo);
        for (Project proj : projectInfos) {
            bo = new DataAuditHeadBO();
            bo.setLabel1(proj.getProjectName() + "("+proj.getProjectUnit()+")");
            bo.setProp(proj.getProjectCode());
            headList.add(bo);
        }
        return headList;
    }
    
    public static List<DataAuditHeadBO> getDataAuditMonthHead(List<String> days){
        List<DataAuditHeadBO> headList = new ArrayList<>();
        DataAuditHeadBO bo = null;
        int length = 0;
        for (String day : days) {
            bo = new DataAuditHeadBO();
            length = day.length();
            bo.setLabel1(day.substring(length-2, length));
            bo.setProp(day);
            headList.add(bo);
        }
        
        
        return headList;
    }
    

    public static List<Map<String,DataAuditBO>> initDataAuditBOs(long startMiss, long endMiss, List<Project> projectInfos) {
        List<DataAuditHeadBO> heads = getDataAuditHead(projectInfos);
        List<Map<String,DataAuditBO>> result = new ArrayList<>();
        
        Map<String,DataAuditBO> map = null;
        DataAuditBO bo = null; 
        Calendar calendar =  Calendar.getInstance();
        calendar.setTimeInMillis(startMiss);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        while (calendar.getTimeInMillis() <= endMiss) {
            String time = sdf.format(calendar.getTime());
            map = new HashMap<>();
            result.add(map);
            for (DataAuditHeadBO head : heads) {
                bo = new DataAuditBO();
                bo.setName(head.getProp());
                if("time".equals(head.getProp()))
                    bo.setValue(time+"时");
                map.put(head.getProp(), bo);
            }
            calendar.add(Calendar.HOUR_OF_DAY, 1); 
        }
        return  result;
    }
    
    public static void convertDataAuditBOs(List<Map<String, DataAuditBO>> result, TDataAudit tDataAudit,
            List<Task> tasks) {
        // 解析梳理时间使符合页面数据
        SimpleDateFormat hourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
        String time = hourSdf.format(tDataAudit.getCreateTime()) + "时";
        // 因子数值
        String value = tDataAudit.getProjValue() == null ? "" : tDataAudit.getProjValue().toString();
        String id = tDataAudit.getId();
        String projId = tDataAudit.getProjId();
        DataAuditBO dataAuditBO = null;
        for (Map<String, DataAuditBO> map : result) {
            // 去第一个监测时间字段数据
            if (map.get("time").getValue().equals(time)) {
                for (String key : map.keySet()) {
                    dataAuditBO = map.get(key);
                    if (key.equals(projId)) {
                        dataAuditBO.setId(id);
                        dataAuditBO.setValue(value);
                        dataAuditBO.setColorStatus(getDataAuditStatus(dataAuditBO, tDataAudit));
                        //dataAuditBO.setRemark("-----");
                        dataAuditBO.setFlowId(tDataAudit.getFlowInstanceId());
                        if(tasks != null && tasks.size() > 0) {
                            dataAuditBO.setOperationAudit(true);
                        }
                        break;
                    }

                }
                break;
            }

        }

    }

    private static int getDataAuditStatus(DataAuditBO dataAuditBO, TDataAudit tDataAudit) {
        List<TDataAuditDetail> details = tDataAudit.getAuditDetailsList();
        Set<String> statusSet = new HashSet<>();
        String three = null;
        String two = null;
        String one = null;
        boolean complete = false;
        String comStatus = null;
        dataAuditBO.setRemark(tDataAudit.getXtAuditDesc());
        for (TDataAuditDetail tDataAuditDetail : details) {
            dataAuditBO.setRemark(tDataAuditDetail.getAuditDesc());
            statusSet.add(tDataAuditDetail.getAuditStatus());
            if(tDataAuditDetail.getAuditStatus().equals(AuditStatus.INVALIDE.getStatusCode())) {
                two = AuditStatus.INVALIDE.getStatusCode();
            }
            if(tDataAuditDetail.getAuditStatus().equals(AuditStatus.DOUBT.getStatusCode())) {
                three = AuditStatus.DOUBT.getStatusCode();
            }
            if(tDataAuditDetail.getAuditStatus().equals(AuditStatus.VALIDE.getStatusCode())) {
                one = AuditStatus.VALIDE.getStatusCode();
            }
            if(tDataAuditDetail.getTaskName().equals(Constants.THREE_AUDIT_CLASS)) {
                complete = true;
                comStatus = tDataAuditDetail.getAuditStatus();
            }
        }
        if(!complete &&  statusSet.size() == 2) {
            dataAuditBO.setRemark("差异");
            return Integer.valueOf(AuditStatus.DIFFERENCE.getStatusCode());
        }
        if(complete && AuditStatus.VALIDE.getStatusCode().equals(comStatus)) {
            return Integer.valueOf(AuditStatus.AUDITED.getStatusCode());
        }
        if(two != null) {
            return Integer.valueOf(two); 
        }
        if(three != null) {
            return Integer.valueOf(three); 
        }
        if(one != null) {
            return Integer.valueOf(one); 
        }
        return Integer.valueOf(tDataAudit.getXtAudit());
    }
}
